<?php
//
//	file: includes/attach/class_attach_display.php
//	author: ptirhiik
//	begin: 05/03/2006
//	version: 1.6.3 - 06/08/2007
//	license: http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
//
//	based on : attachment_mod copyright (c) 2002 Meik Sievertsen (acydburn)
//

if ( !defined('IN_PHPBB') )
{
	die('Hacking attempt');
	exit;
}

class attach_display extends plug_ins
{
	var $posts;
	var $data;
	var $update_views;
	var $handler;

	function attach_display()
	{
		global $config;

		$this->posts = false;
		$this->data = array();
		$this->update_views = array();
		$this->handler = new file_handler($config->globals['mod_attachment_CH']['config']->data);
	}

	function set_posts(&$posts)
	{
		$this->posts = &$posts;
	}

	function topic_fields(&$topic_fields)
	{
		$topic_fields[] = 'topic_attachment';
		return true;
	}

	function read()
	{
		global $config;

		$is_attach = $this->posts->topic['topic_attachment'] && !$this->posts->shorten && (!$this->posts->review || intval($config->globals['mod_attachment_CH']['config']->data['attachment_topic_review']));
		$postrow = &$this->posts->data;
		$forum_id = $this->posts->forum_id;
		return $this->plug_read($is_attach, &$postrow, $forum_id);
	}

	function is_auth($forum_id=0)
	{
		global $user;
		return empty($forum_id) || $user->auth(POST_FORUM_URL, 'auth_mod', $forum_id) || ($user->auth(POST_FORUM_URL, 'auth_download', $forum_id) && $user->auth(POST_FORUM_URL, 'auth_read', $forum_id));
	}

	function plug_read($switch_attachment, &$postrow, $forum_id=0)
	{
		global $db, $config, $user;

		$this->data = array();
		$this->update_views = array();

		// no attached files, no posts, or attachmod inactive
		if ( !intval($switch_attachment) || empty($postrow) || intval($config->globals['mod_attachment_CH']['config']->data['disable_mod']) )
		{
			return;
		}

		// auth
		if ( !$this->is_auth($forum_id) )
		{
			return;
		}

		// get post ids
		$post_ids = array();
		if ( empty($forum_id) )
		{
			foreach ( $postrow as $dummy => $data )
			{
				if ( intval($data['post_attachment']) && $this->is_auth($data['forum_id']) )
				{
					$post_ids[] = intval($data['post_id']);
				}
			}
		}
		else
		{
			$post_ids = array_keys($postrow);
		}
		$this->data = array();
		if ( !empty($post_ids) )
		{
			$sql = 'SELECT a.post_id, d.*
						FROM ' . ATT_LINKS_TABLE . ' a, ' . ATT_DESC_TABLE . ' d
						WHERE d.attach_id = a.attach_id
							AND a.privmsgs_id = 0
							AND a.post_id IN(' . implode(', ', array_map('intval', $post_ids)) . ')
						ORDER BY a.post_id, d.filetime' . (intval($config->globals['mod_attachment_CH']['config']->data['display_order']) ? '' : ' DESC');
			$result = $db->sql_query($sql, false, __LINE__, __FILE__);
			while ( $row = $db->sql_fetchrow($result) )
			{
				$this->data[ intval($row['post_id']) ][ intval($row['attach_id']) ] = $row;
			}
			$db->sql_freeresult($result);
		}
		$config->globals['mod_attachment_CH']['extensions']->read();
		$this->display_main();
	}

	function display_main()
	{
		global $user, $config, $template;

		$template->assign_vars(array(
			'L_DESCRIPTION' => $user->lang('Description'),
			'L_DOWNLOAD' => $user->lang('Download'),
			'L_FILENAME' => $user->lang('File_name'),
			'L_FILESIZE' => $user->lang('Filesize'),
			'L_POSTED_ATTACHMENTS' => $user->lang('Posted_attachments'),
		));
	}

	function display($post_id=0, $tpl_switch='')
	{
		global $db, $config, $user, $template;

		if ( empty($this->data[$post_id]) )
		{
			return;
		}
		foreach ( $this->data[$post_id] as $attach_id => $data )
		{
			// Admin is allowed to view forbidden Attachments, but the error-message is displayed too to inform the Admin
			$denied = false;
			if ( !isset($config->globals['mod_attachment_CH']['extensions']->data[ $data['extension'] ]) )
			{
				$denied = true;
				$template->assign_block_vars('attach', array(
					'L_DENIED' => sprintf($user->lang('Extension_disabled_after_posting'), $data['extension']),
				));
				$template->set_switch('attach.denyrow');
			}

			if ( !$denied || ($user->data['user_level'] == ADMIN) )
			{
				// Some basic things...
				$filename = basename($data['physical_filename']);
				$thumbnail_filename = THUMB_DIR . '/t_' . basename($data['physical_filename']);
				$display_name = basename($data['real_filename']);
				$file_comment = str_replace("\n", '<br />', $data['file_comment']);
				$download_mode = isset($config->globals['mod_attachment_CH']['extensions']->data[ $data['extension'] ]) ? intval($config->globals['mod_attachment_CH']['extensions']->data[ $data['extension'] ]['download_mode']) : INLINE_LINK;
				$i_upload = !empty($config->globals['mod_attachment_CH']['extensions']->data[ $data['extension'] ]['upload_icon']) ? $config->globals['mod_attachment_CH']['extensions']->data[ $data['extension'] ]['upload_icon'] : (!empty($config->globals['mod_attachment_CH']['config']->data['upload_img']) ? $config->globals['mod_attachment_CH']['config']->data['upload_img'] : false);
				$l_upload = !empty($config->globals['mod_attachment_CH']['extensions']->data[ $data['extension'] ]['ext_group_name']) ? $config->globals['mod_attachment_CH']['extensions']->data[ $data['extension'] ]['ext_group_name'] : $data['extension'];
				$u_upload = $this->handler->get_download_url($filename, false);
				$width = intval($data['width']);
				$height = intval($data['height']);

				$units = array();
				$unit = size_get_units(intval($data['filesize']), $units);
				$filesize = round(intval($data['filesize']) / $unit, 2);
				$size_lang = $units[$unit];

				// define category
				$display_cat = '';
				switch ( $config->globals['mod_attachment_CH']['extensions']->data[ $data['extension'] ]['cat_id'] )
				{
					case IMAGE_CAT:
						$display_cat = 'link';
						if ( intval($config->globals['mod_attachment_CH']['config']->data['img_display_inlined']) )
						{
							if ( !intval($config->globals['mod_attachment_CH']['config']->data['img_link_width']) && !intval($config->globals['mod_attachment_CH']['config']->data['img_link_height']) )
							{
								$display_cat = 'image';
							}
							else
							{
								if ( ($width < 0) || ($height < 0) )
								{
									list($width, $height) = $this->image_size(IMAGE_CAT, $attach_id, $filename);
								}
								if ( $width && $height && (!intval($config->globals['mod_attachment_CH']['config']->data['img_link_width']) || ($width <= intval($config->globals['mod_attachment_CH']['config']->data['img_link_width']))) && (!intval($config->globals['mod_attachment_CH']['config']->data['img_link_height']) || ($height <= intval($config->globals['mod_attachment_CH']['config']->data['img_link_height']))) )
								{
									$display_cat = 'image';
								}
							}
						}
						if ( $data['thumbnail'] == 1 )
						{
							$display_cat = 'thumbnail';
						}
						break;

					case STREAM_CAT:
						$display_cat = 'stream';
						break;

					case SWF_CAT:
						$display_cat = 'swf';
						if ( ($width < 0) || ($height < 0) )
						{
							list($width, $height) = $this->image_size(SWF_CAT, $attach_id, $filename);
						}
						if ( !$width || !$height )
						{
							$display_cat = 'link';
						}
						break;

					default:
						$display_cat = 'link';
						break;
				}

				// let's go with display
				$tpl_vars = array(
					'FILESIZE' => $filesize,
					'SIZE_VAR' => $user->lang($size_lang),
					'COMMENT' => $file_comment,
					'DOWNLOAD_NAME' => $display_name,
					'I_UPLOAD' => $i_upload ? $user->img_styled($i_upload) : '',
					'L_UPLOAD' => $l_upload,
					'L_DOWNLOAD_COUNT' => sprintf($user->lang('Download_number'), $data['download_count']),
				);
				switch ( $display_cat )
				{
					case 'image':
						if ( ($download_mode == PHYSICAL_LINK) && !$u_upload )
						{
							$download_mode = INLINE_LINK;
						}
						$download_link = ($download_mode != PHYSICAL_LINK);

						// send to template
						$tpl_vars += array(
							'L_DOWNLOADED_VIEWED' => $user->lang('Viewed'),
							'IMG_SRC' => $download_link ? $config->url('viewtopic', array('mode' => 'attach', 'id' => $attach_id), true) : $u_upload,
						);

						// Directly Viewed Image ... update the download count
						if ( !$download_link )
						{
							$this->update_views[] = intval($attach_id);
						}
						break;

					case 'thumbnail':
						if ( ($download_mode == PHYSICAL_LINK) && !($thumbnail_filename = $this->handler->get_download_url($thumbnail_filename, false)) )
						{
							$download_mode = INLINE_LINK;
						}
						$download_link = ($download_mode != PHYSICAL_LINK);

						// send to template
						$tpl_vars += array(
							'L_DOWNLOADED_VIEWED' => $user->lang('Viewed'),
							'IMG_SRC' =>  $config->url('viewtopic', array('mode' => 'attach', 'id' => $attach_id), true),
							'IMG_THUMB_SRC' => $download_link ? $config->url('viewtopic', array('mode' => 'attach', 'id' => $attach_id, 'thumb' => 1), true) : $thumbnail_filename,
						);
						break;

					case 'stream':
						// Streams
						$tpl_vars += array(
							'L_DOWNLOADED_VIEWED' => $user->lang('Viewed'),
							'U_DOWNLOAD_LINK' => $u_upload,
							'S_AUTOPLAY' => isset($config->globals['mod_attachment_CH']['config']->data['wma_autoplay']) && intval($config->globals['mod_attachment_CH']['config']->data['wma_autoplay']),
						);

						// Viewed/Heared File ... update the download count (download.php is not called here)
						$this->update_views[] = intval($attach_id);
						break;

					case 'swf':
						// Macromedia Flash Files
						$tpl_vars += array(
							'L_DOWNLOADED_VIEWED' => $user->lang('Viewed'),
							'U_DOWNLOAD_LINK' => $u_upload,
							'S_AUTOPLAY' => isset($config->globals['mod_attachment_CH']['config']->data['flash_autoplay']) && intval($config->globals['mod_attachment_CH']['config']->data['flash_autoplay']),
							'WIDTH' => $width,
							'HEIGHT' => $height,
						);

						// Viewed/Heared File ... update the download count (download.php is not called here)
						$this->update_views[] = intval($attach_id);
						break;

					case 'link':
						// display attachment
						$tpl_vars += array(
							'L_DOWNLOADED_VIEWED' => $user->lang('Downloaded'),
							'U_DOWNLOAD_LINK' => $config->url('viewtopic', array('mode' => 'attach', 'id' => $attach_id), true),
							'TARGET_BLANK' => ' target="phpbbattach"',
						);
						break;
				}

				// send all to template
				$template->assign_block_vars('attach', $tpl_vars);
				$template->set_switch('attach.cat_' . $display_cat);
				$template->set_switch('attach.cat_' . $display_cat . '.upload');
				$template->set_switch('attach.cat_' . $display_cat . '.upload.img', $i_upload);
				$template->set_switch('attach.cat_' . $display_cat . '.target_blank', isset($tpl_vars['TARGET_BLANK']) && !empty($tpl_vars['TARGET_BLANK']));
				$template->set_switch('attach.cat_' . $display_cat . '.autoplay', isset($tpl_vars['S_AUTOPLAY']) && intval($tpl_vars['S_AUTOPLAY']));
				$template->set_switch('attach.cat_' . $display_cat . '.dimensions', isset($tpl_vars['WIDTH']) && $width && $height);
			}
		}
		if ( empty($tpl_switch) )
		{
			$template->assign_vars(array(
				'ATTACHMENTS' => $template->include_file('attach_display_box.tpl', array('attach')),
			));
		}
		else
		{
			$template->assign_lastblock_vars($tpl_switch, array(
				'ATTACHMENTS' => $template->include_file('attach_display_box.tpl', array('attach')),
			));
		}
	}

	function update_views()
	{
		global $db;

		if ( !empty($this->update_views) )
		{
			$sql = 'UPDATE ' . ATT_DESC_TABLE . '
						SET download_count = download_count + 1
						WHERE attach_id IN(' . implode(', ', $this->update_views) . ')';
			$db->sql_query($sql, false, __LINE__, __FILE__);
		}
	}

	function image_size($cat_id, $attach_id, $filename)
	{
		global $db, $config;

		if ( !class_exists('image_standard') )
		{
			include($config->url('includes/attach/class_image'));
		}
		if ( $cat_id == IMAGE_CAT )
		{
			$image = new image_standard($this->handler);
		}
		else
		{
			$image = new image_flash($this->handler);
		}
		list($width, $height) = $image->image_size($this->handler->path($filename));
		unset($image);
		$fields = array(
			'width' => $width,
			'height' => $height,
		);
		$sql = 'UPDATE ' . ATT_DESC_TABLE . '
					SET ' . $db->sql_fields('update', $fields) . '
					WHERE attach_id = ' . intval($attach_id);
		$db->sql_query($sql, false, __LINE__, __FILE__);

		return array($width, $height);
	}
}

class attach_display_privmsg extends attach_display
{
	function is_auth()
	{
		global $config, $user;
		return ($user->data['user_level'] == ADMIN) || intval($config->globals['mod_attachment_CH']['config']->data['allow_pm_attach']);
	}

	function plug_read($switch_attachment, &$postrow, $forum_id=0)
	{
		global $db, $config, $user;

		$this->data = array();
		$this->update_views = array();

		// no attached files, no pm, or attachmod inactive
		if ( !intval($switch_attachment) || empty($postrow) || intval($config->globals['mod_attachment_CH']['config']->data['disable_mod']) )
		{
			return;
		}

		// auth
		if ( !$this->is_auth($forum_id) )
		{
			return;
		}
		$this->data = array();
		if ( $postrows_id = intval($postrow['privmsgs_id']) )
		{
			$sql = 'SELECT a.privmsgs_id, d.*
						FROM ' . ATT_LINKS_TABLE . ' a, ' . ATT_DESC_TABLE . ' d
						WHERE d.attach_id = a.attach_id
							AND a.post_id = 0
							AND a.privmsgs_id = ' . intval($postrows_id) . '
						ORDER BY d.filetime' . (intval($config->globals['mod_attachment_CH']['config']->data['display_order']) ? '' : ' DESC');
			$result = $db->sql_query($sql, false, __LINE__, __FILE__);
			while ( $row = $db->sql_fetchrow($result) )
			{
				$this->data[0][ intval($row['attach_id']) ] = $row;
			}
			$db->sql_freeresult($result);
		}

		$config->globals['mod_attachment_CH']['extensions']->read();
		$this->display_main();
	}

	function display_main()
	{
		global $user, $config, $template;

		parent::display_main();

		$count_data = count($this->data);
		$template->set_switch('switch_attachments', $count_data);
		if ( $count_data )
		{
			$template->assign_vars(array(
				'L_DELETE_ATTACHMENTS'	=> $user->lang($count_data == 1 ? 'Delete_attachment' : 'Delete_attachments'),
			));
		}
	}
}

?>